package com.zjf.bbs.util;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.upload.FormFile;

import com.zjf.bbs.databean.UploadBean;
import com.zjf.bbs.entity.HeadPic;

public class UploadUtil {
	public static final String[] imageExts = {".gif",".jpg",".jpeg",".png",".tmp"};
	public static final String[] fileExts = {".rar",".zip"};
	@SuppressWarnings("deprecation")
	public static UploadBean doUpload(FormFile formfile,String path,HttpServletRequest request) throws FileNotFoundException, IOException{
		String ext = HeadPic.getExt(formfile.getFileName());
		String code = UUID.randomUUID().toString();
		String filename = code+ext;
		byte[] filedata = formfile.getFileData();
		String realPath = request.getRealPath(path);
		File dir = new File(realPath);
		if(!dir.exists()){
			dir.mkdirs();
		}
		String filePath =  realPath+File.separator+filename;
		String vPath = request.getContextPath()+path+File.separator+filename;
		FileOutputStream fos = new FileOutputStream(new File(filePath));
		fos.write(filedata);
		fos.close();
		UploadBean bean = new UploadBean();
		bean.setCode(code);
		bean.setvPath(vPath);
		return bean;
		
	}
	@SuppressWarnings("deprecation")
	public static UploadBean doUploadByTime(FormFile formfile,String path,HttpServletRequest request) throws FileNotFoundException, IOException{
		String ext = HeadPic.getExt(formfile.getFileName());
		String fmt = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date())+new Random().nextInt(1000);
		String code = fmt;
		String filename = code+ext;
		byte[] filedata = formfile.getFileData();
		String realPath = request.getRealPath(path);
		File dir = new File(realPath);
		if(!dir.exists()){
			dir.mkdirs();
		}
		String filePath =  realPath+File.separator+filename;
		String vPath = request.getContextPath()+path+filename;
		FileOutputStream fos = new FileOutputStream(new File(filePath));
		fos.write(filedata);
		fos.close();
		UploadBean bean = new UploadBean();
		bean.setCode(code);
		bean.setvPath(vPath);
		return bean;
		
	}
	public static boolean validateUpload(FormFile formfile,String[] allowTypes){
		String filename = formfile.getFileName();
		String ext = HeadPic.getExt(filename);
		for(String t:allowTypes){
			if(t.toLowerCase().equals(ext.toLowerCase())){
				return true;
			}
		}
		return false;
	}
	public static boolean validateUploadImage(FormFile formfile){
		if(validateUpload(formfile, imageExts))
			return true;
		else 
			return false;
	}
	public static boolean validateUploadFile(FormFile formfile){
		if(validateUpload(formfile, fileExts))
			return true;
		else 
			return false;
	}
	public static boolean validateUploadAll(FormFile formfile){
		if(validateUploadFile(formfile) || validateUploadImage(formfile)){
			return true;
		}
		else{
			return false;
		}
	}
}
